package com.example.views;

import java.util.ArrayList;

import com.example.R;
import com.example.R.layout;
import com.example.R.menu;
import com.example.model.Date;
import com.example.model.Item;
import com.example.presenters.ItemListPresenter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * This activity will handle all the displaying of items for a user. It will
 * allow a user to use a filter or a search term to display all appropriate
 * items on a list. The user will then be able to click on a item and go to that
 * item's page to view more information.
 * 
 * @author 3Factorial
 */
public class ItemListActivity extends Activity {

	private ItemListPresenter presenter;
	private ListView itemList;

	private static String[] EMPTY_LIST = { "No results." };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_list);
		presenter = new ItemListPresenter();
		itemList = (ListView) findViewById(R.id.item_display_list);
		setListAdapter(presenter.getAllItems());
		TextView tv = (TextView) findViewById(R.id.dateErrorMessage);
		tv.setVisibility(TextView.INVISIBLE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.item_list, menu);
		return true;
	}

	/**
	 * Sets the ListView with the appropriate ListAdapter. If the item array
	 * list is empty, it will make the ListView show a "No results" prompt.
	 * 
	 * @param items
	 *            Items to display through the ListView widget on the screen.
	 */
	public void setListAdapter(ArrayList<Item> items) {
		if (items == null || items.isEmpty()) {
			itemList.setAdapter(new ArrayAdapter<String>(this,
					R.layout.simplerow, ItemListActivity.EMPTY_LIST));
		} else {
			itemList.setAdapter(new ArrayAdapter<Item>(this,
					R.layout.simplerow, items));
		}
	}

	/**
	 * Applies all the checked filters to the list, displaying only the items
	 * that match all checked boxes.
	 */
	public void applyFilters(View view) {
		ArrayList<String> filters = getFilters();
		ArrayList<Item> matches = presenter.getMatchedItems(filters);
		setListAdapter(matches);
	}

	/**
	 * Removes all filters from the list, displaying all items.
	 */
	public void removeFilters(View view) {
		setListAdapter(presenter.getAllItems());
	}

	/**
	 * Filters the list by the status of the item, i.e., whether it was lost or
	 * found.
	 * 
	 * @param view
	 *            Activity view to use with this method.
	 */
	public void filterByStatus(View view) {
		switch (view.getId()) {
		case R.id.statusLost:
			setListAdapter(presenter.getAllLostItems());
			// TODO Added breaks. Make sure code still works with these here!!!
			break;
		case R.id.statusFound:
			setListAdapter(presenter.getAllFoundItems());
			break;
		}
	}

	/**
	 * Filters the item list by date, with items from the start date to the end
	 * date.
	 * 
	 * @param view
	 */
	public void dateFilter(View view) {
		DatePicker start = (DatePicker) findViewById(R.id.datePickerStart);
		DatePicker end = (DatePicker) findViewById(R.id.datePickerEnd);
		Date startDate = getDate(start);
		Date endDate = getDate(end);
		if (endDate.isBefore(startDate)) {
			TextView tv = (TextView) findViewById(R.id.dateErrorMessage);
			tv.setText("Invalid Date Range");
			tv.setVisibility(TextView.VISIBLE);
		} else {
			ArrayList<Item> filtered = presenter.sortByDate(startDate, endDate);
			setListAdapter(filtered);
		}

	}

	/**
	 * Searches the list for items matching the given name or category.
	 * 
	 * @param view
	 */
	public void searchItems(View view) {
		RadioButton nameBox = (RadioButton) findViewById(R.id.search_by_name_radio_button);
		RadioButton categoryBox = (RadioButton) findViewById(R.id.search_by_category_radio_button);
		String search = ((EditText) findViewById(R.id.search_text_box))
				.getText().toString();
		ArrayList<Item> searched;
		if (nameBox.isChecked()) {
			searched = presenter.getNamedItems(search);
			setListAdapter(searched);
		} else if (categoryBox.isChecked()) {
			ArrayList<String> tags = new ArrayList<String>();
			tags.add(search);
			searched = presenter.getMatchedItems(tags);
			setListAdapter(searched);
		}
	}

	/**
	 * Creates a date using the information provided by the given date picker.
	 * 
	 * @param d
	 * @return The date the date picker was set up to retrieve.
	 */
	public Date getDate(DatePicker d) {
		int day = d.getDayOfMonth();
		int month = d.getMonth();
		int year = d.getYear();
		return (new Date(day, month, year));
	}

	/**
	 * Retrieves a list of all checked filters as strings.
	 * 
	 * @return All checked filters as strings
	 */
	public ArrayList<String> getFilters() {

		ArrayList<String> checkedFilters = new ArrayList<String>();

		CheckBox clothesBox = (CheckBox) findViewById(R.id.item_display_clothes_checkbox);
		CheckBox electronicsBox = (CheckBox) findViewById(R.id.item_display_electronics_checkbox);
		CheckBox equipmentBox = (CheckBox) findViewById(R.id.item_display_equipment_checkbox);
		CheckBox furnitureBox = (CheckBox) findViewById(R.id.item_display_furniture_checkbox);
		CheckBox housewaresBox = (CheckBox) findViewById(R.id.item_display_housewares_checkbox);
		CheckBox hygieneBox = (CheckBox) findViewById(R.id.item_display_hygiene_checkbox);
		CheckBox petsBox = (CheckBox) findViewById(R.id.item_display_pets_checkbox);
		CheckBox toysBox = (CheckBox) findViewById(R.id.item_display_toys_checkbox);
		CheckBox otherBox = (CheckBox) findViewById(R.id.item_display_other_checkbox);

		if (clothesBox.isChecked())
			checkedFilters.add("Clothes");

		if (electronicsBox.isChecked())
			checkedFilters.add("Electronics");

		if (equipmentBox.isChecked())
			checkedFilters.add("Equipment");

		if (furnitureBox.isChecked())
			checkedFilters.add("Furniture");

		if (housewaresBox.isChecked())
			checkedFilters.add("Housewares");

		if (hygieneBox.isChecked())
			checkedFilters.add("Hygiene");

		if (petsBox.isChecked())
			checkedFilters.add("Pets");

		if (toysBox.isChecked())
			checkedFilters.add("Toys");

		if (otherBox.isChecked())
			checkedFilters.add("Other");

		return checkedFilters;
	}

}
